// src/stores/settingsStore.js
import { defineStore } from 'pinia';

// --- 新增：定义默认值常量，方便重置 ---
const DEFAULTS = {
    DEFAULT_CHUNK_SIZE: 100, // MB
    CONCURRENT_TASKS: 3,     // 任务并发数
    CONCURRENT_CHUNKS: 3,    // 分片并发数
    PRESIGNED_URL_EXPIRATION: 300, // 秒
    URL_UPLOAD_CHUNK_SIZE: 10,
    CHUNK_MAX_RETRIES: 3,        // 每个分片的最大重试次数
    CHUNK_RETRY_DELAY: 2000,     // 首次重试的基础延迟（毫秒）
};

export const useSettingsStore = defineStore('settings', {
    state: () => ({
        // 从 localStorage 读取，如果没有，则使用常量中的默认值
        defaultChunkSize: parseInt(localStorage.getItem('defaultChunkSize') || DEFAULTS.DEFAULT_CHUNK_SIZE, 10),
        concurrentTasks: parseInt(localStorage.getItem('concurrentTasks') || DEFAULTS.CONCURRENT_TASKS, 10),
        concurrentChunks: parseInt(localStorage.getItem('concurrentChunks') || DEFAULTS.CONCURRENT_CHUNKS, 10),
        presignedUrlExpiration: parseInt(localStorage.getItem('presignedUrlExpiration') || DEFAULTS.PRESIGNED_URL_EXPIRATION, 10),
        urlUploadChunkSize: parseInt(localStorage.getItem('urlUploadChunkSize') || DEFAULTS.URL_UPLOAD_CHUNK_SIZE, 10),
        chunkMaxRetries: parseInt(localStorage.getItem('chunkMaxRetries') || DEFAULTS.CHUNK_MAX_RETRIES, 10),
        chunkRetryDelay: parseInt(localStorage.getItem('chunkRetryDelay') || DEFAULTS.CHUNK_RETRY_DELAY, 10),
    }),
    actions: {
        // --- 为每个设置项提供独立的 action ---
        setDefaultChunkSize(sizeInMB) {
            this.defaultChunkSize = sizeInMB;
            localStorage.setItem('defaultChunkSize', sizeInMB.toString());
        },
        setConcurrentTasks(count) {
            this.concurrentTasks = count;
            localStorage.setItem('concurrentTasks', count.toString());
        },
        setConcurrentChunks(count) {
            this.concurrentChunks = count;
            localStorage.setItem('concurrentChunks', count.toString());
        },
        setPresignedUrlExpiration(seconds) {
            this.presignedUrlExpiration = seconds;
            localStorage.setItem('presignedUrlExpiration', seconds.toString());
        },
        setUrlUploadChunkSize(sizeInMB) {
            this.urlUploadChunkSize = sizeInMB;
            localStorage.setItem('urlUploadChunkSize', sizeInMB.toString());
        },
        setChunkMaxRetries(count) {
            this.chunkMaxRetries = count;
            localStorage.setItem('chunkMaxRetries', count.toString());
        },
        setChunkRetryDelay(ms) {
            this.chunkRetryDelay = ms;
            localStorage.setItem('chunkRetryDelay', ms.toString());
        },

        // --- 【新增】恢复默认值的 action ---
        resetToDefaults() {
            this.setDefaultChunkSize(DEFAULTS.DEFAULT_CHUNK_SIZE);
            this.setConcurrentTasks(DEFAULTS.CONCURRENT_TASKS);
            this.setConcurrentChunks(DEFAULTS.CONCURRENT_CHUNKS);
            this.setPresignedUrlExpiration(DEFAULTS.PRESIGNED_URL_EXPIRATION);
            this.setUrlUploadChunkSize(DEFAULTS.URL_UPLOAD_CHUNK_SIZE);
            this.setChunkMaxRetries(DEFAULTS.CHUNK_MAX_RETRIES);
            this.setChunkRetryDelay(DEFAULTS.CHUNK_RETRY_DELAY);
        }
    },
});